/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import model.pojo.User;
import model.pojo.util.helper;

/**
 *
 * @author Yasith
 */
public class fbUser {

    private static String admin_username;
    private static String admin_password;

    public static User getUser(Integer userID) {
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("User.findByUserID");
        query.setParameter("userID", userID);
        User u;
        try {
            u = (User) query.getSingleResult();
        } catch (NoResultException ex) {
            u = null;
        }
        newTx.commit();
        newEm.close();

        return u;
    }

    public enum LoginValidation {

        Succesful,
        InvalidEmail,
        WrongPassword
    }

    public static LoginValidation ValidateLogin(String email, String password) {
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("User.findByEmail");
        query.setParameter("email", email);
        User u;
        try {
            u = (User) query.getSingleResult();
        } catch (NoResultException ex) {
            u = null;
        }
        newTx.commit();
        newEm.close();

        if (u != null) {
            if (u.getPwd().equals(password)) {
                return LoginValidation.Succesful;
            } else {
                return LoginValidation.WrongPassword;
            }
        } else {
            return LoginValidation.InvalidEmail;
        }

    }

    public static User getUser(String email) {

        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("User.findByEmail");
        query.setParameter("email", email);
        User u;
        try {
            u = (User) query.getSingleResult();
        } catch (NoResultException ex) {
            u = null;
        }
        newTx.commit();
        newEm.close();

        return u;
    }

    public static List<User> getUsers() {

        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createQuery("Select u FROM User u ORDER BY u.displayName");
        List<User> u;
        try {
            u = (List<User>) query.getResultList();
        } catch (NoResultException ex) {
            u = null;
        }
        newTx.commit();
        newEm.close();

        return u;
    }

    public static boolean addUser(String email, String username, String password, String accntStatus) {

        EntityManager em = helper.getEMF().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        User u = new User();
        u.setDisplayName(username);
        u.setPwd(password);
        u.setEmail(email);
        u.setAccountStatus(accntStatus);
        u.setCreationDate(new Date());

        em.persist(u);
        tx.commit();
        em.close();

        return true;
    }

    public static boolean removeUser(int userID) {
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("User.findByUserID");
        query.setParameter("userID", userID);
        User u;
        try {
            u = (User) query.getSingleResult();
        } catch (NoResultException ex) {
            u = null;
        }
        newEm.remove(u);

        newTx.commit();
        newEm.close();

        return true;
    }

    public static boolean isAdmin(User u) {

        if (u.getDisplayName().equals(admin_username) && u.getPwd().equals(admin_password)) {
            return true;
        } else {
            return false;
        }
    }

    public static void setAdmin_username(String ausername) {
        admin_username = ausername;
    }

    public static void setAdmin_password(String apassword) {
        admin_password = apassword;
    }
}
